package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.x.protobuf.Mysqlx;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account")
@SuppressWarnings({"all"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;


	/**
	 * 获取消费记录
	 * /api/account/userAccount/findUserConsumePage/{page}/{limit}
	 * @param page
	 * @param limit
	 * @return
	 */
	@GetMapping("/userAccount/findUserConsumePage/{page}/{limit}")
	@GuiGuLogin
	public Result<Page<UserAccountDetail>> findUserConsumePage(@PathVariable Long page, @PathVariable Long limit) {
		Long userId = AuthContextHolder.getUserId();
		Page<UserAccountDetail> userAccountDetailPage = new Page<>(page, limit);
		String tradeType = SystemConstant.ACCOUNT_TRADE_TYPE_MINUS;
		userAccountDetailPage = userAccountService.findUserAccountDetailPage(userAccountDetailPage, userId, tradeType);
		return Result.ok(userAccountDetailPage);
	}

	/**
	 * 获取充值记录
	 * /api/account/userAccount/findUserRechargePage/{page}/{limit}
	 * @param page
	 * @param limit
	 * @return
	 */
	@GetMapping("/userAccount/findUserRechargePage/{page}/{limit}")
	@GuiGuLogin
	public Result<Page<UserAccountDetail>> findUserRechargePage(@PathVariable Long page, @PathVariable Long limit) {
		Long userId = AuthContextHolder.getUserId();
		Page<UserAccountDetail> userAccountDetailPage = new Page<>(page, limit);
		String tradeType = SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT;
		userAccountDetailPage = userAccountService.findUserAccountDetailPage(userAccountDetailPage, userId, tradeType);
		return Result.ok(userAccountDetailPage);
	}


	/**
	 * /api/account/userAccount/checkAndLock
	 * 检查及锁定账户金额
	 * @param accountLockVo
	 * @return
	 */
	@PostMapping("/userAccount/checkAndLock")
	public Result checkAndLock(@RequestBody AccountLockVo accountLockVo) {
		userAccountService.checkAndLock(accountLockVo);
		return Result.ok();
	}

	/**
	 * /api/account/userAccount/getAvailableAmount
	 * 获取账户可用余额
	 * @return
	 */
	@GetMapping("/userAccount/getAvailableAmount")
	@GuiGuLogin
	public Result<BigDecimal> getAvailableAmount(){
		Long userId = AuthContextHolder.getUserId();
		UserAccount byId = userAccountService.getById(userId);
		BigDecimal availableAmount = byId.getAvailableAmount();
		return Result.ok(availableAmount);
	}
}

